﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using TTBusiness.DB_Linq;
namespace TTBusiness.ClassDB
{
    /// <summary>
    /// Create by ThiepND 23/3/2011
    /// </summary>
    public class clsBSCateNews
    {
        TTMarketing_DBDataContext objDB = new TTMarketing_DBDataContext();
        public List<tblCateNew> getAll()
        {
            try
            {
                var list = from a in objDB.tblCateNews
                           select a;
                return list.ToList<tblCateNew>();
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<tblCateNew> getByTypeAndStatus(int typeid,bool status)
        {
            try
            {
                var list = from a in objDB.tblCateNews
                           where a.Status.Equals(status) && a.TypeID.Equals(typeid)
                           select a;
                return list.ToList<tblCateNew>();
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<tblCateNew> getByStatus(bool status)
        {
            try
            {
                var list = from a in objDB.tblCateNews
                           where a.Status.Equals(status)
                           select a;
                return list.ToList<tblCateNew>();
            }
            catch (Exception)
            {

                throw;
            }
        }
        public tblCateNew getById(int id)
        {
            try
            {
                var obj = objDB.tblCateNews.SingleOrDefault(a => a.ID == id);
                return obj;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public tblCateNew getByName(string name)
        {
            try
            {
                var obj = objDB.tblCateNews.SingleOrDefault(a => a.Name == name);
                return obj;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public void insert(tblCateNew obj)
        {
            try
            {
                objDB.tblCateNews.InsertOnSubmit(obj);
                objDB.SubmitChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }

        public void update(tblCateNew obj)
        {
            try
            {
                var objj = objDB.tblCateNews.SingleOrDefault(a => a.ID == obj.ID);
                if (objj != null)
                {
                    objj.ID = obj.ID;
                    objj.Name = obj.Name;
                    objj.Number = obj.Number;
                    objj.Status = obj.Status;
                    objj.Visible = obj.Visible;
                    objj.TypeID = obj.TypeID;
                    objDB.SubmitChanges();
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        public void updateStatus(bool status, int id)
        {
            try
            {
                var objj = objDB.tblCateNews.SingleOrDefault(a => a.ID == id);
                if (objj != null)
                {
                    objj.Status = status;
                    objDB.SubmitChanges();
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        public void delete(int id)
        {
            try
            {
                var obj = (from a in objDB.tblCateNews
                           where a.ID == id
                           select a).SingleOrDefault();
                objDB.tblCateNews.DeleteOnSubmit(obj);
                objDB.SubmitChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
